﻿@using Website.CRUDServices
@using Location = Website.Models.Domain.Location
@using Profile = Website.Models.Domain.Profile
@using Region = Website.Models.Domain.Region
@model Activity
@{
    this.ViewBag.Title = "Forretningsudvikling";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var Regions = this.ViewData["Regions"] as List<Region>;
    var allTopics = this.ViewData["DiscussionTopics"] as List<Topic>;
    var Projects = this.ViewData["Projects"] as List<Project>;
    var Profiles = this.ViewData["Profiles"] as Website.Models.ViewModels.ProfilesView;
    var BDActivity = this.Model as Activity;
    Guid otherId = Guid.Empty;

    // errorhandling
    var errorMsg = new HtmlString(string.Empty);
    if (this.ViewData["ErrorMessage"] != null)
    {
        errorMsg = this.ViewData["ErrorMessage"] as HtmlString;
    }

}

@Html.Partial("~/Views/_Shared/LogView.cshtml", Model.ID)

<script type="text/javascript">
    //toggle script
    function toggle(id) {
        if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == "") {
            document.getElementById(id).style.display = 'block';
        } else {
            document.getElementById(id).style.display = 'none';
        }
    }
</script>
<!--toggle additional comments -->
@if (BDActivity.BDA.AdditionalComments != null && BDActivity.BDA.AdditionalComments != "")
{
    <script>
        $(function () {
            toggle('Comments_holder');
        });
    </script>
}
<!--toggle topic elaboration -->
 @foreach (Topic top in allTopics.Where(x => (BDActivity.BDA.Topics ?? new Guid[0]).Contains(x.ID)))
 {
     if (top.Title == "Andet")
     {
                        <script>
                            $(function () {
                                toggle('andet_holder');
                            });
                        </script>
     }

 }

<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>

@using (this.Html.BeginForm())
{
    <div class="error">
        @errorMsg
    </div>
    // hidden field to hold ID of the object
    <input type="hidden" id="ID" name="ID"  readonly="readonly" value="@BDActivity.ID" />
    // hidden field to hold ID of the objects object
    <input type="hidden" id="ID2" name="ID2"  readonly="readonly" value="@BDActivity.BDA.ID" />

    // date
    <div class="formBullet">Dato</div>
    <input type="text" id="datepicker" name="datepicker" readonly="readonly" value="@BDActivity.TimeOfActivity" />

    // location
    <div class="formBullet">Lokation*</div>
    <div class="formEntry">
        <select name="Location" id="Locations">
            <option value="@Guid.Empty">Vælg en lokation</option>
            @foreach (Location loc in Profiles.Locations.OrderBy(x => x.Name))
            {
                if (loc.ID == BDActivity.Location)
                {
                <option value="@loc.ID" selected="selected">@loc.Name</option>
                }
                else
                {
                <option value="@loc.ID">@loc.Name</option>
                }
            }
        </select>
        
    </div>

            // ownerProjects chooser
    <div class="formBullet">Alle projekter tilknyttet </div>
    <div class="formEntry">
        <select multiple="multiple" name="OwnerProjects" id="OwnerProjects">
            @foreach (Project proj in Projects.Where(x => (BDActivity.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID" selected="selected">@proj.Name</option>
            }
            @foreach (Project proj in Projects.Where(x => !(BDActivity.OwnerProjects ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@proj.ID">@proj.Name</option>
            }
        </select>
    </div>

            // profiles chooser
    <div class="formBullet">Profiler</div>
    <div class="formEntry">
        <select multiple="multiple" name="profilesSelect" id="profilesSelect">
            @foreach (Profile prof in Profiles.Profiles.Where(x => (BDActivity.Attendees ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID" selected="selected">@prof.Name</option>
            }
            @foreach (Profile prof in Profiles.Profiles.Where(x => !(BDActivity.Attendees ?? new Guid[0]).Contains(x.ID)))
            {
                <option value="@prof.ID">@prof.Name</option>
            }
        </select>
    </div>


            // talks chooser
    <div class="formBullet">Specificeret snak</div>
    <div class="formEntry">
        <select multiple="multiple" name="talksSelect" id="talksSelect">
            @foreach (Topic top in allTopics.Where(x => (BDActivity.BDA.Topics ?? new Guid[0]).Contains(x.ID)))
            {
                if (top.Title == "Andet")
                {
                    otherId = top.ID;
                }

                <option value="@top.ID" selected="selected">@top.Title</option>
            }
            @foreach (Topic top in allTopics.Where(x => !(BDActivity.BDA.Topics ?? new Guid[0]).Contains(x.ID)))
            {
                if (top.Title == "Andet")
                {
                    otherId = top.ID;
                }

                <option value="@top.ID">@top.Title</option>
            }
        </select>
    </div>
    <br />
    <br />

            // field for specifying "andet"
    <div class="formEntry open_hidden" id="andet_holder">
        <div class="formBullet">Specificér andet</div>
        <textarea name="Andet">@BDActivity.BDA.TopicElaboration</textarea>
    </div>

    
    


            // status
    <div class="formBullet">Beskrivelse</div>
    <div class="formEntry">
        <textarea name="Status">@BDActivity.Commentary</textarea>
    </div>
    
            // additional comments
    <div class="formBullet">
        @if (BDActivity.BDA.AdditionalComments == null || BDActivity.BDA.AdditionalComments == "")
        {
            <input type="checkbox" onclick=" toggle('Comments_holder'); " name="comments_check" value="Ja" />
        }
        else
        {
            <input type="checkbox" onclick=" toggle('Comments_holder'); " name="comments_check" value="Ja" checked/>
        }
Yderligere kommentarer? <br/>
    </div>
    <div class="formEntry open_hidden" id="Comments_holder">
        <textarea name="Comments">@BDActivity.BDA.AdditionalComments</textarea>
    </div>
    
    
    <div class="buttonHolder">
        <script type="text/javascript">
            function confirmDelete() {
                if (confirm("Er du sikker på at du vil slette aktiviteten? Dette valg kan ikke fortrydes.")) {
                    location.href = '@this.Url.Action("DeleteBDA", new { ID = BDActivity.ID })';
                }
            }
        </script>
            <input type="button" value="Slet" class="deleteButton margin-right-20" 
                 onclick="confirmDelete();"
                />
        <input type="submit" value="Gem" />
    </div>
}

<script>

    $(function () {
        $('#profilesSelect').multiSelect();
        $('#OwnerProjects').multiSelect();
        /*$("#newLocation").click(function () {
            $("#dialog").dialog({
                width: "340px",
                modal: true
            });
        });*/
        //choosing "andet" will pop up an additional text-box for input on the subject

        $('#talksSelect').multiSelect({
            afterSelect: function (value) {
                if (value == "@otherId.ToString()") {
                    toggle("andet_holder");
                }
            },

            afterDeselect: function (value) {
                if (value == "@otherId.ToString()") {
                    toggle("andet_holder");
                }
            }
        });
        $('#CreateLocationForm').submit(function (e) {
            e.preventDefault();
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        // We have a JSON object in case of success
                        $('#Locations')
                            .append($("<option></option>")
                                .attr("value", result.locationID)
                                .text(result.locationName))
                            .val(result.locationID);


                        $("#dialog").dialog('close');
                    } else {
                        // We have the partial with errors in case of failure
                        // so all we have to do is update the DOM
                        $('#CreateLocationForm').html(result);
                    }
                }
            });
        });
    });
</script>

<div id="dialog" title="Opret ny lokation" style="display: none;">
    @Html.Partial("~/Views/Locations/_CreateLocationPartial.cshtml", new Location())
</div>
